package com.cyw.note.manage.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cyw.common.core.domain.PageQuery;
import com.cyw.common.core.page.TableDataInfo;
import com.cyw.common.utils.StringUtils;
import com.cyw.note.common.RespBean;
import com.cyw.note.manage.domain.Type;
import com.cyw.note.manage.domain.bo.TypeBo;
import com.cyw.note.manage.domain.vo.TypeVo;
import com.cyw.note.manage.mapper.TypeMapper;
import com.cyw.note.manage.service.ITypeService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 文章类型Service业务层处理
 *
 * @author 二叔
 * @date 2022-08-07
 */
@RequiredArgsConstructor
@Service
public class TypeServiceImpl implements ITypeService {

    private final TypeMapper baseMapper;

    /**
     * 查询文章类型
     */
    @Override
    public TypeVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询文章类型列表
     */
    @Override
    public TableDataInfo<TypeVo> queryPageList(TypeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Type> lqw = buildQueryWrapper(bo);
        Page<TypeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询文章类型列表
     */
    @Override
    public List<TypeVo> queryList(TypeBo bo) {
        LambdaQueryWrapper<Type> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Type> buildQueryWrapper(TypeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Type> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), Type::getName, bo.getName());
        return lqw;
    }

    /**
     * 新增文章类型
     */
    @Override
    public Boolean insertByBo(TypeBo bo) {
        Type add = BeanUtil.toBean(bo, Type.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改文章类型
     */
    @Override
    public Boolean updateByBo(TypeBo bo) {
        Type update = BeanUtil.toBean(bo, Type.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Type entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除文章类型
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public RespBean getAllType() {
        return RespBean.success(baseMapper.getAllType());
    }
}
